//
//  HMDatePicker.m
//  01-网易彩票
//
//  Created by SZSYKT_iOSBasic_2 on 16/2/20.
//  Copyright © 2016年 heima. All rights reserved.
//

#import "HMDatePicker.h"
#import "HMKeyboardTool.h"

@interface HMDatePicker()
// 工具条
@property (nonatomic, weak) HMKeyboardTool *tool;
// 日期选择器
@property (nonatomic, weak) UIDatePicker *datePicker;
// 遮盖按钮
@property (nonatomic, strong) UIButton *coverBtn;
@end

@implementation HMDatePicker

/**
 *  创建日期选中器
 */
+(instancetype)datePicker {
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
         [self setup];
    }
    return self;
}

/**
 *  添加子控件
 */
- (void)setup {
    // 创建工具条
    HMKeyboardTool *tool = [HMKeyboardTool tool];
    // 设置block
    tool.btnClickBlock = ^(HMKeyboardToolButtonType buttonType){
        // 隐藏当前view
        [self dismiss];
        if (buttonType == HMKeyboardToolButtonTypeCancel) { // 取消
            self.datePicker.date = _currentDate;
        }
        // 通知代理
        [self datePickerValueChanged];
    };
    // 将工具条添加到当前view
    [self addSubview:tool];
    self.tool = tool;
    
    // 创建日期选择器
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    // 设置时区
//    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    datePicker.locale = [NSLocale systemLocale];
    // 设置时间模式
    datePicker.datePickerMode = UIDatePickerModeTime;
    // 监听日期值改变事件
    [datePicker addTarget:self action:@selector(datePickerValueChanged) forControlEvents:UIControlEventValueChanged];
    // 添加到当前view
    [self addSubview:datePicker];
    self.datePicker = datePicker;
}

- (void)setCurrentDate:(NSDate *)currentDate {
    _currentDate = currentDate;
    self.datePicker.date = currentDate;
}

/**
 *  监听日期值改变事件
 */
- (void)datePickerValueChanged {
    if ([self.delegate respondsToSelector:@selector(datePickerDidValueChanged:)]) {
        [self.delegate datePickerDidValueChanged:self];
    }
}

/**
 *  布局子控件
 */
- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 设置工具条的frame
    CGFloat width = self.frame.size.width;
    CGFloat toolH = 40;
    self.tool.frame = CGRectMake(0, 0, width, toolH);
    // 设置日期选择器的frame
    CGFloat dateX = 0;
    CGFloat dateY = toolH;
    self.datePicker.frame = CGRectMake(dateX, dateY, width, 216);
}


#pragma mark - 显示和隐藏方法
/**
 *  显示
 */
- (void)show {
    // 获得应用程序主窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGFloat screenH = CGRectGetHeight(window.frame);
    CGFloat screenW = CGRectGetWidth(window.frame);
    // 设置当前view的frame
    CGFloat height = 256;
    self.frame = CGRectMake(0, screenH, screenW, height);
    
    // 将当前view添加到窗口上
    [window addSubview:self];
    
    // 创建遮盖按钮
    self.coverBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH - height)];
    self.coverBtn.backgroundColor = [UIColor clearColor];
    [self.coverBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    // 将按钮添加到窗口上
    [window addSubview:self.coverBtn];
    
    // 以动画的形式显示出来
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.frame;
        frame.origin.y = screenH - height;
        self.frame = frame;
    }];
}
/**
 *  隐藏
 */
- (void)dismiss{
    // 获得应用程序主窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGFloat screenH = CGRectGetHeight(window.frame);
    // 以动画的形式隐藏日期选择器
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.frame;
        frame.origin.y = screenH;
        self.frame = frame;
    } completion:^(BOOL finished) {
        // 移除当前控件
        [self removeFromSuperview];
        // 将遮盖按钮移除
        [self.coverBtn removeFromSuperview];
        self.coverBtn = nil;
    }];
}



@end
